<template>
  <div class="search-result">
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
      :error.sync="error"
      error-text="请求失败，点击重新加载"
    >
      <van-cell v-for="(item,i) in list" :key="i" :title="item.title">
      </van-cell>
    </van-list>
  </div>
</template>

<script>
// 防抖处理
import { getSearchResult } from '@/api/search'
export default {
  name: 'SearchResult',
  components: {},
  props: {
    searchText: {
      type: String,
      required: true
    }
  },
  data () {
    return {
      error: false,
      list: [],
      loading: false,
      finished: false,
      page: 1,
      perPage: 20
    }
  },
  computed: {},
  watch: {
  },
  created () {
  },
  mounted () {},
  methods: {
    async onLoad () {
      try {
        // 1.调接口
        const { data } = await getSearchResult({
          page: this.page,
          per_page: this.perPage,
          q: this.searchText
        })
        // 2.渲染数据
        const { results } = data.data
        this.list.push(...results)
        // 3.设置加载状态
        this.loading = false
        // 4.判断添加，结束加载
        if (results.lenght) {
          this.page++
        } else {
          this.finished = true
        }
      } catch (err) {
        this.error = true
        this.loading = false
        console.log('获取数据失败', err)
      }
    }
  }
}
</script>

<style scoped lang="less">
// 深度作用dom改变样式
  /deep/ .van-cell__title{
    span.active{
       color: red !important;
    }
}

</style>
